/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing.jee.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import testing.jee.util.DateUtils;
import testing.jee.util.HolidayUtils;

/**
 *
 * @author kreisera
 */
public class Timeline {

    private User user;
    List<Date> dates = new ArrayList<Date>();
    private Map<Date, Workday> workdays = new HashMap<Date, Workday>();

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Workday getWorkday(Date date) {
        return workdays.get(date);
    }

    public void setWorkday(Date date, Workday workday) {
        workdays.put(date, workday);
    }

    public void addWorkday(Workday workday) {
        workdays.put(workday.getDate(), workday);
    }

    public List<Date> getDates() {
        return dates;
    }

    public static Timeline getTimeline(Date startDate, Date endDate) {
        Timeline timeline = new Timeline();
        startDate = DateUtils.normalize(startDate);
        endDate = DateUtils.normalize(endDate);
        Date currentDate = startDate;
        while (!currentDate.after(endDate)) {
            Workday workday = new Workday();
            workday.setDate(currentDate);
            if (HolidayUtils.isHoliday(currentDate)) {
                workday.setHoliday(true);
            }
            timeline.dates.add(currentDate);
            timeline.addWorkday(workday);
            currentDate = DateUtils.toNextWorkDay(currentDate);
        }
        return timeline;
    }

    public static Timeline getTimeline(Date startDate, int numberOfDays) {
        Timeline timeline = new Timeline();
        startDate = DateUtils.normalize(startDate);
        Date currentDate = startDate;
        int currentDay = 0;
        while (currentDay < numberOfDays) {
            Workday workday = new Workday();
            workday.setDate(currentDate);
            if (HolidayUtils.isHoliday(currentDate)) {
                workday.setHoliday(true);
            }
            timeline.dates.add(currentDate);
            timeline.addWorkday(workday);
            currentDate = DateUtils.toNextWorkDay(currentDate);
            currentDay++;
        }
        return timeline;
    }
}